#!/bin/bash
# C环境配置文件

# 交叉编译配置
CROSS_COMPILE=false
TARGET_ARCH="native"
TOOLCHAIN_PREFIX=""
TOOLCHAIN_PATH=""
SYSROOT=""

# 编译器配置
# 默认使用本地编译器
export CC=gcc
export CXX=g++
export AR=ar
export AS=as
export LD=ld
export NM=nm
export OBJCOPY=objcopy
export OBJDUMP=objdump
export RANLIB=ranlib
export READELF=readelf
export STRIP=strip

# 如果启用交叉编译，则使用交叉编译工具链
if [[ "$CROSS_COMPILE" == true ]]; then
    # 如果指定了工具链路径，添加到PATH
    if [[ -n "$TOOLCHAIN_PATH" ]]; then
        export PATH="$TOOLCHAIN_PATH/bin:$PATH"
    fi
    
    # 设置交叉编译工具链
    if [[ -n "$TOOLCHAIN_PREFIX" ]]; then
        export CC="${TOOLCHAIN_PREFIX}gcc"
        export CXX="${TOOLCHAIN_PREFIX}g++"
        export AR="${TOOLCHAIN_PREFIX}ar"
        export AS="${TOOLCHAIN_PREFIX}as"
        export LD="${TOOLCHAIN_PREFIX}ld"
        export NM="${TOOLCHAIN_PREFIX}nm"
        export OBJCOPY="${TOOLCHAIN_PREFIX}objcopy"
        export OBJDUMP="${TOOLCHAIN_PREFIX}objdump"
        export RANLIB="${TOOLCHAIN_PREFIX}ranlib"
        export READELF="${TOOLCHAIN_PREFIX}readelf"
        export STRIP="${TOOLCHAIN_PREFIX}strip"
    else
        # 使用默认前缀
        case "$TARGET_ARCH" in
            arm64)
                export CC=aarch64-linux-gnu-gcc
                export CXX=aarch64-linux-gnu-g++
                export AR=aarch64-linux-gnu-ar
                export STRIP=aarch64-linux-gnu-strip
                ;;
            arm)
                export CC=arm-linux-gnueabihf-gcc
                export CXX=arm-linux-gnueabihf-g++
                export AR=arm-linux-gnueabihf-ar
                export STRIP=arm-linux-gnueabihf-strip
                ;;
            riscv)
                export CC=riscv64-linux-gnu-gcc
                export CXX=riscv64-linux-gnu-g++
                export AR=riscv64-linux-gnu-ar
                export STRIP=riscv64-linux-gnu-strip
                ;;
        esac
    fi
    
    # 设置目标三元组（从前缀推导）
    export TARGET_TRIPLET="${TOOLCHAIN_PREFIX%-}"
    
    # 设置系统根目录
    if [[ -n "$SYSROOT" ]]; then
        export SYSROOT="$SYSROOT"
        export PKG_CONFIG_SYSROOT_DIR="$SYSROOT"
        export PKG_CONFIG_LIBDIR="$SYSROOT/usr/lib/pkgconfig:$SYSROOT/usr/share/pkgconfig"
    fi
fi

# 编译选项
export CFLAGS="-Wall -Wextra -std=c99 -O2"
export CXXFLAGS="-Wall -Wextra -std=c++11 -O2"

# 交叉编译特定选项
if [[ "$CROSS_COMPILE" == true ]]; then
    case "$TARGET_ARCH" in
        arm64)
            export CFLAGS="$CFLAGS -march=armv8-a -mtune=cortex-a72"
            export CXXFLAGS="$CXXFLAGS -march=armv8-a -mtune=cortex-a72"
            ;;
        arm)
            export CFLAGS="$CFLAGS -march=armv7-a -mfpu=neon -mfloat-abi=hard"
            export CXXFLAGS="$CXXFLAGS -march=armv7-a -mfpu=neon -mfloat-abi=hard"
            ;;
        riscv)
            export CFLAGS="$CFLAGS -march=rv64gc -mabi=lp64d"
            export CXXFLAGS="$CXXFLAGS -march=rv64gc -mabi=lp64d"
            ;;
        x86_64)
            export CFLAGS="$CFLAGS -march=x86-64 -mtune=generic"
            export CXXFLAGS="$CXXFLAGS -march=x86-64 -mtune=generic"
            ;;
    esac
    
    # 链接选项
    export LDFLAGS="-Wl,--hash-style=gnu"
fi

# CMake工具链文件路径
export CMAKE_TOOLCHAIN_FILE="$(dirname "$BASH_SOURCE")/../cross-compile/cmake/${TARGET_ARCH}_${TOOLCHAIN:-gcc}.cmake"

echo "C环境已配置:"
echo "  编译模式: $([ "$CROSS_COMPILE" == true ] && echo "交叉编译" || echo "本地编译")"
echo "  目标架构: $TARGET_ARCH"
echo "  CC: $CC"
echo "  CXX: $CXX"
echo "  CFLAGS: $CFLAGS"
echo "  CXXFLAGS: $CXXFLAGS"
if [[ "$CROSS_COMPILE" == true ]]; then
    if [[ -n "$TOOLCHAIN_PREFIX" ]]; then
        echo "  编译器前缀: $TOOLCHAIN_PREFIX"
        echo "  目标三元组: $TARGET_TRIPLET"
    fi
    if [[ -n "$SYSROOT" ]]; then
        echo "  系统根目录: $SYSROOT"
    fi
    if [[ -n "$TOOLCHAIN_PATH" ]]; then
        echo "  工具链路径: $TOOLCHAIN_PATH"
    fi
fi
